package com.giw.project.file_manager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class SplitFileById {
	private final static File fInput = new File(
			"/home/giorgio/Scrivania/GIW/progetto/0-grafo/Split");
	private String out_path = "/home/giorgio/Scrivania/GIW/progetto/0-grafo/Split/newSplit";
	
	private final static int element4file = 1000;
	
	//ricorda che type deve essere maiuscolo!!!
	public void run(String type) {
		// sottocartella tipo
		out_path += "/"+type;
		makeDir(out_path);
		
		try {
			FileInputStream fisInput = new FileInputStream(fInput+"/"+type+".txt");
			InputStreamReader isrInput = new InputStreamReader(fisInput);
			BufferedReader brInput = new BufferedReader(isrInput);
			String lineaInput = brInput.readLine();
			
			while (lineaInput != null) {

				String currentNode = lineaInput.split("\t")[0];
				String[] parts_uri = currentNode.split("/");
				String tmp_id = parts_uri[(parts_uri.length-1)];
				
				// verifica visiva funzionamento
//				System.out.println(tmp_id.substring(0,tmp_id.length()-1));
				
				int id = Integer.parseInt(tmp_id.substring(0,tmp_id.length()-1));
				int current_out_id = id/element4file;
				
//				System.out.println(out_path);
				String dirName = out_path+"/"+Integer.toString(current_out_id);
//				System.out.println(dirName);
				String fileName = dirName+"/"+type+Integer.toString(current_out_id)+"_file";
//				System.out.println(out_path);
				
				//creazione cartella
				makeDir(dirName);
				
				int new_out_id = current_out_id;
				while (lineaInput != null && new_out_id==current_out_id) {
					FileWriter fw = new FileWriter(fileName+".txt",true);
					PrintWriter pw = new PrintWriter(fw);
					
					pw.println(lineaInput);
					pw.close();
					
					lineaInput = brInput.readLine();
					if (lineaInput!=null) {
						currentNode = lineaInput.split("\t")[0];
						parts_uri = currentNode.split("/");
						tmp_id = parts_uri[(parts_uri.length-1)];

						// verifica visiva funzionamento
//						System.out.println("sono dentro");

						id = Integer.parseInt(tmp_id.substring(0,tmp_id.length()-1));
						new_out_id = id/element4file;
						System.out.println(new_out_id);
						System.out.println(current_out_id);
						System.out.println();
						fw.close();
					}
				}
			}

			brInput.close();
			isrInput.close();
			fisInput.close();

		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}
	
	private static void makeDir(String dirName) {
		boolean success = (new File(dirName).mkdir());
		if(success) {
			System.out.println(dirName + " -> created");
		} else {
			System.out.println("Impossible to create -> "+dirName);
		}
	}

	public static void main(String[] args) throws FileNotFoundException {
		SplitFileById s = new SplitFileById();
		s.run("genre"); // si fa cos�?
	}
}
